{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "requests.Request 类是 Python 的 requests 库中的一个核心类,用于创建 HTTP 请求。它提供了一种更低级的接口来构建 HTTP 请求,相比于直接使用 requests.get() 或 requests.post() 等高级函数,requests.Request 类给予了更多的控制和灵活性。\n",
    "\n",
    "下面是 requests.Request 类的一些主要特性和用法:\n",
    "\n",
    "### 初始化参数:\n",
    "* url: 请求的 URL。\n",
    "* method: HTTP 方法,如 'GET'、'POST'、'PUT' 等。\n",
    "* data: 要发送的请求体数据,可以是字典、字节或文件对象。\n",
    "* headers: 要发送的 HTTP 头部字典。\n",
    "* cookies: 要发送的 Cookie 字典。\n",
    "* files: 要上传的文件字典。\n",
    "* auth: 身份验证凭证,可以是 (username, password) 元组或 AuthBase 对象。\n",
    "* params: URL 查询参数字典。\n",
    "* json: 要发送的 JSON 数据。\n",
    "* proxies: 代理服务器设置。\n",
    "* timeout: 请求超时时间。\n",
    "* allow_redirects: 是否允许重定向。\n",
    "* verify: 是否验证 SSL 证书。\n",
    "* cert: 用于 SSL 客户端认证的证书文件路径。\n",
    "\n",
    "### 发送请求:\n",
    "* 使用 requests.Session.send() 方法发送 requests.Request 对象创建的请求:\n",
    "```python\n",
    "from requests import Request, Session\n",
    "\n",
    "req = Request('GET', 'https://example.com')\n",
    "prepared = req.prepare()\n",
    "\n",
    "with Session() as s:\n",
    "    response = s.send(prepared)\n",
    "```\n",
    "* 也可以使用 requests.request() 函数直接发送 requests.Request 对象创建的请求:\n",
    "```python\n",
    "from requests import request\n",
    "\n",
    "req = Request('GET', 'https://example.com')\n",
    "prepared = req.prepare()\n",
    "response = request(prepared)\n",
    "```\n",
    "\n",
    "### 附加功能:\n",
    "* prepare() 方法会创建一个 PreparedRequest 对象,进一步处理请求参数并准备发送。\n",
    "* 可以通过 hooks 参数添加自定义的请求/响应钩子函数。\n",
    "* 可以设置 auth、cookies、headers、params 等属性来动态修改请求。\n",
    "\n",
    "### 应用场景:\n",
    "* 在构建复杂的 HTTP 客户端时,requests.Request 类提供了更细粒度的控制,可以更好地满足特定的需求。\n",
    "* 当需要手动构建和修改请求头部、cookies、身份验证等信息时,使用 requests.Request 类会更加方便。\n",
    "* 在需要自定义请求处理逻辑(如添加钩子函数)时,requests.Request 类也更加适用。"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
